<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addFn(1,'0')">添加权限</el-button>
        </div>
        <!-- 添加row-key="id"属性用来渲染树型数据 -->
        <el-table :data="list" row-key="id" border>
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button size="small" type="text" @click="addFn(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="editFn(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="title" @close="closeFn">
        <!-- 表单内容 -->
        <el-form ref="addForm" :model="formData" label-width="100px">
          <el-form-item prop="name" label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item prop="code" label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              style="display: block"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#dcdfe6"
              active-text="开启"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="onsubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, addPermissionList, delPermissionApi, editPermissionApi, onePermissionApi } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      showDialog: false,
      list: [],
      formData: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: true
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑弹框' : '新增弹框'
    }
  },
  created() {
    this.getPermissionFn()
  },
  methods: {
    // 回显
    async editFn(id) {
      this.showDialog = true
      const res = await onePermissionApi({ id: id })
      this.formData = res.data
      console.log(res, '225555')
      // this.formData = res
    },
    // 删除
    delFn(id) {
      this.$confirm('你确定要删除吗', '温馨提示').then(async() => {
        await delPermissionApi(id)
        this.$message.success('删除成功')
        this.getPermissionFn()
      })
    },
    // 关闭
    closeFn() {
      this.showDialog = false
      this.$refs.addForm.resetFields()
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: true
      }
    },
    // 提交
    onsubmit() {
      this.$refs.addForm.validate(async(flag) => {
        if (this.formData.id) {
          await editPermissionApi(this.formData)
          this.$message.success('修改成功')
        } else {
          const res = await addPermissionList(this.formData)
          console.log(res)
          this.$message.success('添加成功')
        }
        this.closeFn()
        this.getPermissionFn()
      })
    },
    async addFn(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 渲染列表
    async getPermissionFn() {
      const res = await getPermissionList()
      this.list = res.data
      this.list = transListToTreeData(res.data, '0')
      console.log(res)
    }
  }
}
</script>
